package com.xmkj.jijidriver.count;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.cons.Constans;
import com.common.jiji.entity.LoginBean;
import com.common.jiji.methods.LoginMethods;
import com.common.jiji.parms.LoginParms;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.DataCenter;
import com.common.retrofit.entity.bean.UserDetailsBean;
import com.common.retrofit.methods.UserInfoMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.common.utils.MD5Utils;
import com.common.utils.SPUtils;
import com.xmkj.jijidriver.MainBizActivity;
import com.xmkj.jijidriver.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by chen on 2018/3/22.
 */

public class LoginActivtiy extends BaseMvpActivity {
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_psw)
    EditText mEtPsw;
    @BindView(R.id.tv_register)
    TextView mTvRegister;
    @BindView(R.id.tv_forget_psw)
    TextView mTvForgetPsw;
    @BindView(R.id.tv_intrue)
    TextView mTvIntrue;
    @BindView(R.id.flag)
    TextView mFlag;
    @BindView(R.id.iv_qq_login)
    ImageView mIvQqLogin;
    @BindView(R.id.iv_weichat_login)
    ImageView mIvWeichatLogin;
    private String jpushid = "";

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {

        attachClickListener(mTvForgetPsw);
        attachClickListener(mTvRegister);
        attachClickListener(mTvIntrue);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == mTvForgetPsw.getId()) {
            gotoActivity(FindMyPswActivity.class);
        } else if (view.getId() == mTvRegister.getId()) {
            gotoActivity(RegisterActivity.class);
        } else if (view.getId() == mTvIntrue.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(mEtPhone))) {
                showToastMsg("请输入账号");
                return;
            } else if (EmptyUtils.isEmpty(getEditTextStr(mEtPsw))) {
                showToastMsg("请输入密码");
                return;
            }
            gotoLogin();
        }
    }

    private void gotoLogin() {
        showProgressingDialog();
        CommonSubscriber<LoginBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                LoginBean infoBean = (LoginBean) o;
                DataCenter.getInstance().setToken(infoBean.result.token);
                System.out.println("infobena"+infoBean.result.toString());
                SPUtils.setShareString(DataCenter.TOKEN,infoBean.result.token);
                dismissProgressDialog();
                onBackPressed();
                gotoActivity(MainBizActivity.class);
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        String registrationID = JPushInterface.getRegistrationID(this);
        LoginParms loginParms = new LoginParms();
        loginParms.deviceId = registrationID;
        loginParms.deviceType = "android";
        loginParms.phone = getEditTextStr(mEtPhone);
        loginParms.password = MD5Utils.MD5Encode(getEditTextStr(mEtPsw));
        LoginMethods.getInstance().loginUser(subscriber,loginParms);
        rxManager.add(subscriber);
    }
    private void getUserInfo(int uid ,String hashid) {
        CommonSubscriber<UserDetailsBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                UserDetailsBean info = (UserDetailsBean) o;
                SPUtils.setShareString(Constans.DETAILS_ID, info.detail_id);

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        UserInfoMethods.getInstance().userInfo(subscriber, uid, hashid);
        rxManager.add(subscriber);
    }
    @Override
    protected void setStatusBar() {

    }


    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }
}
